package com.example.administrator.jingpingou.shop_activity;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

import com.example.administrator.jingpingou.R;
import com.example.administrator.jingpingou.adapter.ShopDataAdapter;
import com.example.administrator.jingpingou.adapter.ShopDataAdapterTwo;
import com.example.administrator.jingpingou.application.MyApplication;
import com.example.administrator.jingpingou.bean.Shop_Data;
import com.example.administrator.jingpingou.config.NetConfig;
import com.example.administrator.jingpingou.netinteface.NetInterface;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CameraActivity extends AppCompatActivity {
    private View mView;
    private RelativeLayout mRL;
    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRecycleView;

    private GridLayoutManager mManagerOne,mManagerTwo;
    private RecyclerView.Adapter mAdapterOne,mAdapterTwo;
    private List<Shop_Data.ListYan> mData;
    private Retrofit mRetrofit;
    private NetInterface mNetInterface;
    private Intent intent;

    private Toolbar mToolbar;
    
    private FragmentManager mFragmentManager;
    private Fragment fragment;

    private ImageView mIVBack,mIVChange,mImgLoad;

    private int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        mIVBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mIVChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(count%2==1) {
                    mRecycleView.setLayoutManager(mManagerOne);
                    mRecycleView.setAdapter(mAdapterOne);
                    count++;
                }else{
                    mRecycleView.setLayoutManager(mManagerTwo);
                    mRecycleView.setAdapter(mAdapterTwo);
                    count++;
                }
            }
        });
    }

    private void setData() {
        mRecycleView.setLayoutManager(mManagerOne);
        mRecycleView.setAdapter(mAdapterOne);
    }

    private void initData() {

        mImgLoad.setBackgroundResource(R.drawable.wait_shop);
        AnimationDrawable animationDrawable = (AnimationDrawable) mImgLoad.getDrawable();
        animationDrawable.start();

        String path = intent.getStringExtra("path");

        mData = new ArrayList<>();
        mRetrofit = new Retrofit.Builder().baseUrl(NetConfig.SHOP_MAIN_DATA_PATH)
                .addConverterFactory(GsonConverterFactory.create()).build();
        mNetInterface = mRetrofit.create(NetInterface.class);
        Call<ResponseBody> shopMainData = mNetInterface.getShopMainData(path);
        shopMainData.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if(response.isSuccessful()) {
                    try {
                        String string = response.body().string();
                        Log.d("TAG", "onResponse: ========"+string);
                        MyApplication application = (MyApplication) getApplication();
                        String str = application.decodeUnicode(string);
                        Log.d("TAG", "onResponse: ========"+str);
                        Gson gson = new Gson();
                        Shop_Data shop_data = gson.fromJson(str, Shop_Data.class);
                        List<Shop_Data.ListYan> list = shop_data.getList();
                        mData.addAll(list);
                        mImgLoad.setVisibility(View.GONE);
                        Log.d("TAG", "onResponse: ========"+mData);
                        mAdapterOne.notifyDataSetChanged();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
        mAdapterOne = new ShopDataAdapter(mData,this);
        mAdapterTwo = new ShopDataAdapterTwo(mData,this);
        mManagerOne = new GridLayoutManager(this,2, GridLayoutManager.VERTICAL,false);
        mManagerTwo = new GridLayoutManager(this,1, GridLayoutManager.VERTICAL,false);


    }

    private void initView() {

        Toolbar toolbar = (Toolbar) findViewById(R.id.tb_toolbar_test);
        if (toolbar==null){
            setSupportActionBar(toolbar);
        }
        mToolbar = (Toolbar) findViewById(R.id.tb_toolbar_test);

        mRL = (RelativeLayout) findViewById(R.id.rl_shop_camera_show);
        mRecycleView = (RecyclerView) findViewById(R.id.rv_camera_content);
        mIVBack = (ImageView) findViewById(R.id.iv_shop_two_back);
        mIVChange = (ImageView) findViewById(R.id.iv_shop_two_change);
        mImgLoad = (ImageView) findViewById(R.id.iv_shop_head_camera);
        intent = getIntent();
        String title = intent.getStringExtra("title");
        mToolbar.setTitle(title);
    }


}
